use crate::database::Database;
use anyhow::Result;
use std::path::PathBuf;

/// Application state that will be managed by Tauri
pub struct AppState {
    pub database: Database,
    pub data_dir: PathBuf,
}

impl AppState {
    pub fn new(data_dir: PathBuf) -> Result<Self> {
        // Ensure data directory exists
        if !data_dir.exists() {
            std::fs::create_dir_all(&data_dir)?;
        }

        // Initialize database
        let db_path = data_dir.join("app.db");
        let database = Database::new(&db_path)?;

        // Run database migrations
        database.run_migrations()?;

        Ok(Self {
            database,
            data_dir,
        })
    }

    pub fn get_data_dir(&self) -> &PathBuf {
        &self.data_dir
    }

    pub fn get_database(&self) -> &Database {
        &self.database
    }
}
